package vis.manage.sys;

import vis.core.common.security.AuthOnlyLogin;
import vis.core.common.util.MathKit;
import vis.core.domain.db.SysUser;
import vis.core.domain.search.Psh;
import vis.core.domain.sys.MSG;
import vis.core.domain.sys.SRS;
import vis.core.domain.sys.ServiceException;
import vis.core.domain.sys.UserTokenVo;
import vis.core.domain.sys.config.SysConfig;
import vis.core.service.captcha.CaptchaService;
import vis.core.service.sys.SysLoginService;
import vis.core.service.sys.SysUserService;
import com.alibaba.fastjson2.JSONObject;
import com.mybatisflex.core.paginate.Page;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.redisson.api.RedissonClient;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

@RestController
@RequestMapping("/manage/system/user")
public class SysUserController {
    @Resource
    private RedissonClient redisson;

    @Resource
    private SysConfig sysConfig;

    @Resource
    private SysUserService sysUserService;

    @Resource
    private SysLoginService sysLoginService;

    @Resource
    private CaptchaService captchaService;

    @PostMapping("/managePwdLogin")
    public SRS<Object> managePwdLogin(String paramStr) {
        //私钥校验参数
        JSONObject param = MathKit.rsaIsExpire(paramStr);
        if (param == null) throw new ServiceException(MSG.USER_LOGIN_PASSWORD_RE);

        // 获取请求的User-Agent信息
        if (RequestContextHolder.getRequestAttributes() != null) {
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            String UserAgent = request.getHeader("User-Agent");
            System.out.println("访问客户端：" + UserAgent);
        }

        //检查验证码
        captchaService.checkLogin(param.getString("id"), param.getString("code"));

        //系统已经禁止登录;排除ROOT用户
        boolean accountIsRoot = sysUserService.accountIsRoot(param.getString("account"));
        if (sysConfig.loginIsDisable() && !accountIsRoot)
            throw new ServiceException(MSG.USER_LOGIN_IS_DISABLE);

        UserTokenVo userTokenVO = sysLoginService.managePwdLogin(param.getString("account"), param.getString("password"));

        return SRS.bySuccess(userTokenVO);
    }

    //刷新Token
    @PostMapping("/refreshToken")
    public SRS<UserTokenVo> refreshToken(String refreshToken) {
        UserTokenVo userTokenVO = sysUserService.refreshToken(refreshToken);
        return SRS.bySuccess(userTokenVO);
    }

    @PostMapping("/resetUserPassword")
    public SRS<Boolean> resetUserPassword(String paramStr) throws Exception {
        JSONObject param = MathKit.rsaIsExpire(paramStr);
        if (param == null) throw new ServiceException(MSG.RSA_RE);
        return SRS.bySuccess(sysUserService.resetUserPassword(param.getString("userIds"), param.getString("newPassword")));
    }

    @PostMapping("/resetMyPassword")
    public SRS<Boolean> resetMyPassword(String paramStr) throws Exception {
        JSONObject param = JSONObject.parseObject(MathKit.rsaDecrypt(paramStr));
        if (param == null) throw new ServiceException(MSG.RSA_RE);
        return SRS.bySuccess(sysUserService.resetMyPassword(param.getString("oldPassword"), param.getString("newPassword")));
    }

    @PostMapping("/registerPhone")
    public SRS<Object> registerPhone(String phone, String paramStr) throws Exception {
        JSONObject param = MathKit.rsaIsExpire(paramStr);
        if (param == null) throw new ServiceException(MSG.USER_REGISTER_RE);

        //检查验证码
        String captcha = param.getString("captcha");
        String password = param.getString("password");
        String password2 = param.getString("password2");
        if (captchaService.checkPhoneRegister(phone, captcha))
            throw new ServiceException(MSG.CAPTCHA_ERROR);
        //注册用户
        boolean isSuccess = sysUserService.sysUserRegister("1", phone, null, password, password2, captcha);
        if (isSuccess) captchaService.delPhoneRegister(phone, captcha);

        return SRS.bySuccess(isSuccess);
    }

    @PostMapping("/registerEmail")
    public SRS<Object> registerEmail(String email, String paramStr) throws Exception {
        JSONObject param = MathKit.rsaIsExpire(paramStr);
        if (param == null) throw new ServiceException(MSG.USER_REGISTER_RE);

        //检查验证码
        String captcha = param.getString("captcha");
        String password = param.getString("password");
        String password2 = param.getString("password2");
        if (!captchaService.checkEmailRegister(email, captcha)) throw new ServiceException(MSG.CAPTCHA_ERROR);

        //注册用户
        boolean isSuccess = sysUserService.sysUserRegister("2", null, email, password, password2, captcha);
        if (isSuccess) captchaService.delEmailRegister(email, captcha);

        return SRS.bySuccess(isSuccess);
    }

    @PostMapping("/insert")
    public SRS<Object> insert(SysUser param) {
        boolean add = sysUserService.insert(param);
        return SRS.bySuccess(add);
    }

    @PostMapping("/update")
    public SRS<Object> update(SysUser param) {
        boolean update = sysUserService.update(param);
        return SRS.bySuccess(update);
    }

    @PostMapping("/delete")
    public SRS<Object> delete(String ids) {
        sysUserService.delete(ids);
        return SRS.bySuccess();
    }

    @PostMapping("/page")
    public SRS<Page<SysUser>> page(SysUser sysUser, Psh psh) {
        Page<SysUser> page = sysUserService.page(sysUser, psh);

        return SRS.bySuccess(page);
    }

    @AuthOnlyLogin
    @PostMapping("/userInfo")
    public SRS<Object> systemUserInfo() {
        SysUser sysUser = sysUserService.systemUserInfo();
        return SRS.bySuccess(sysUser);
    }

}
